package com.usian.service.impl;

import com.usian.config.RedisClient;
import com.usian.mapper.TbUserMapper;
import com.usian.pojo.TbUser;
import com.usian.pojo.TbUserExample;
import com.usian.service.SSOService;
import com.usian.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class SSOServiceImpl implements SSOService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisClient redisClient;

    @Override
    public boolean checkUserInfo(String checkValue, int checkFlag) {
        List<TbUser> userList =new ArrayList<>();
        TbUserExample example =new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        //checkFlag  1  用户名   2：手机号
        if(checkFlag==1){
            criteria.andUsernameEqualTo(checkValue);
            userList= tbUserMapper.selectByExample(example);
        }else if(checkFlag==2){
            criteria.andPhoneEqualTo(checkValue);
            userList = tbUserMapper.selectByExample(example);
        }

        //校验有值 返回 false  无值  true
        if(userList.size()>0){
            return  false;
        }
        return true;
    }

    @Override
    public boolean userRegister(TbUser tbUser) {
        String pwd = MD5Utils.digest(tbUser.getPassword());
        tbUser.setPassword(pwd);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        int insert = tbUserMapper.insert(tbUser);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public Map userLogin(String username, String password) {

        String pwd = MD5Utils.digest(password);
        System.out.println("pwd======="+pwd);

        TbUserExample example =new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(pwd);
        List<TbUser> userList = tbUserMapper.selectByExample(example);
        if(userList.size()>0){
            TbUser user = userList.get(0);
            System.out.println("======="+user.getPassword()+"====="+pwd);
            if(pwd.equals(user.getPassword())){

                //生成token
                String token = UUID.randomUUID().toString();

                //token 放到redis
                redisClient.set("USER_INFO:"+token,user);
                redisClient.expire("USER_INFO:"+token,86400L);

                //返回登录用户信息
                Map map =new HashMap();
                map.put("token",token);
                map.put("userid",user.getId());
                map.put("username",user.getUsername());
                return  map;
            }else{
                return null;
            }
        }
        return null;
    }

    @Override
    public int getUserByToken(String token) {

        //token  redis  user
        TbUser tbUser = (TbUser) redisClient.get("USER_INFO:" + token);
        if(tbUser!=null){
            redisClient.set("USER_INFO:"+token,tbUser);
            redisClient.expire("USER_INFO:"+token,86400L);
            return 1;
        }
        return 0;
    }

    @Override
    public int logOut(String token) {

        redisClient.del("USER_INFO:"+token);

        return 1;
    }


}
